from fastapi import APIRouter, Body

from common.pkg.fastapi_util import cbv
from core.view import AdminBaseView
from schema.req_schema.language_req_schema import AddNewLanguageModel, UpdateLanguageRequestModel
from schema.resp_schema.language_schema import ListLanguageResponseModel, AddNewLanguageResponseModel
from service.language_service import LanguageService

router = APIRouter(prefix="/language")


@cbv(router)
class LanguageView(AdminBaseView):

    @router.get("", description="获取当前所有的语言", response_model=ListLanguageResponseModel)
    async def get_language(self):
        ls = LanguageService(self.mgw, self.redis)
        ok, data_or_code = await ls.get_language_list()
        return self.unpack_model_response(ok, data_or_code, response_model=ListLanguageResponseModel)

    @router.post("", description="新增语言", response_model=AddNewLanguageResponseModel)
    async def add_language(self, language_model: AddNewLanguageModel = Body(...)):
        ls = LanguageService(self.mgw, self.redis)
        ok, data_or_code = await ls.add_new_language(language_model)
        return self.unpack_response(ok, data_or_code)

    @router.put("/{language_id:int}", description="修改语言")
    async def update_language(self, req_model: UpdateLanguageRequestModel = Body(...)):
        ...